﻿using System.ServiceModel.DomainServices.Client;
using System.Windows;
using System.Windows.Controls;
using MedLink2011.Views.Patients.ViewModels;
using MedLink2011.Web.Services;

namespace MedLink2011.Views.Patients.ChildWindows
{
    public partial class Inpatient_regsAddCW : ChildWindow
    {
        
        readonly int loadedPatient_Id = MedLink2011.Views.Patients.ViewModels.PatientViewModel.DashLoad_PK;
        //private int loadedPatient_Id = 1;
        private EntityQuery<MedLink2011.Web.Models.Hospital_bed> _hospitalBedQuery;
        private MedLink2011Context _context;
        
        public Inpatient_regsAddCW()
        {
            InitializeComponent();
        LoadComboBoxData();
            
        }


        private void OKButton_Click(object sender, RoutedEventArgs e)
            {
                Patient_IdTextBox.Text = loadedPatient_Id.ToString(); //make sure is not the primary key of the current form, it needs to be a FOREIGN key.
                Hospital_bed_IdTextBox.Text = Hospital_bed_IdComboBox.SelectedIndex.ToString();
            if (!Inpatient_regViewModel._inpatient_regContext.IsLoading)
                {
                    this.Inpatient_regsDataForm.CommitEdit();
                    this.DialogResult = true;
                }
            }
            
        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
        
        private void LoadComboBoxData()
        {

            //Hospital_bed_IdComboBox
             _context = new MedLink2011Context();
            _hospitalBedQuery = _context.GetHospital_bedQuery();
            _hospitalBedQuery = _hospitalBedQuery.OrderBy(e => e.Hospital_bed_Id);
            _context.Load(_hospitalBedQuery, LoadBehavior.RefreshCurrent, Hospital_bed_IdComboBox_Loaded, null);
            Hospital_bed_IdComboBox.ItemsSource = _context.Hospital_beds;
        }


        private void Hospital_bed_IdComboBox_Loaded(LoadOperation<MedLink2011.Web.Models.Hospital_bed> lo)
            {
                this.Hospital_bed_IdComboBox.SelectedIndex = 1;
            }

            private void Inpatient_regsDataForm_EditEnded(object sender, DataFormEditEndedEventArgs e)
            {
                this.Close();
            }

    }
}

